﻿using System;

// 接口来计算面积
public interface IShape
{
    double CalculateArea();
    bool IsValid();
}

// 抽象类，包含形状共有属性，从而简化代码
public abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

// 长方形类
public class Rectangle : Shape
{
    private double width;
    private double height;

    public Rectangle(double width, double height)
    {
        this.width = width;
        this.height = height;
    }

    public override double CalculateArea()
    {
        return width * height;
    }

    public override bool IsValid()
    {
        return width > 0 && height > 0;
    }
}

// 正方形类
public class Square : Shape
{
    private double side1;
    private double side2;

    public Square(double side, double side2)
    {
        this.side1= side;
        this.side2 = side2;
    }

    public override double CalculateArea()
    {
       
        return side1 * side2;
    }

    public override bool IsValid()
    {
        return side1==side2;
    }
}

// 圆形类
public class Circle : Shape
{
    private double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public override double CalculateArea()
    {
        return Math.PI * radius * radius;
    }

    public override bool IsValid()
    {
        return radius > 0;
    }
}

// 程序主类
public class Program
{
    public static void Main()
    {
        Random random = new Random();
        Shape[] shapes = new Shape[10];
        double totalArea = 0;
        string[] name = { "长方形" ,"正方形","圆"};

        for (int i = 0; i < shapes.Length; i++)
        {
            int shapeType = random.Next(3); // 随机选择形状类型（0:长方形，1:正方形，2:圆形）
         
            double size = random.NextDouble() * 10 + 1; // 随机生成尺寸，确保大于0

            Shape shape = CreateShape(shapeType, size);

            if (shape.IsValid())
            {
                totalArea += shape.CalculateArea();
                Console.WriteLine($"图形 {i + 1}合法为{name[shapeType]}, 面积 = {shape.CalculateArea()}");
            }
            else
            {
                Console.WriteLine($"Shape {i + 1} 不合法.");
            }
        }

        Console.WriteLine($"这10个图形的总面积为: {totalArea}");
    }

    // 用于创建形状对象
    private static Shape CreateShape(int shapeType, double size)
    {
        switch (shapeType)
        {
            case 0:
                return new Rectangle(size, size + 25);
            case 1:
                return new Square(size,size);
            case 2:
                return new Circle(size);
            default:
                throw new ArgumentException("目前没有这种形状");
        }
    }
}